/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import RMS.RememberLoadedLevel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

public class DrawChoice extends Canvas {

    public static Image q_nomal, q_hover, q_failed, live;
    public static Image logo, unlock, lock;
    private Image bufferImage;
    private int PAD_BT = 5;
    private int PAD_LR = 10;
    private int choiceWidth, choiceHeight = 30;
    private int getLine = 1;
    private int screen = 1;
    private Start midlet;
    private int firstMenu;
    public static int secondMenu;
    private Graphics g;
    private int startScroll, endScroll1, endScroll2;
    private Font font;
    private int padding_top;
    private int trans, instanceKeyCode;
    public static int level = 1, score, LEVEL, liv, levelPlaying;
    public static boolean tangToc, tt, khoiDong, kd, veDich, vd, level3, ans, bigAnswer, pass;
    private HighScore hScore;
    int levelActivated;
    public static int imgID, demBai;
    public static int[] KD, TT, VD;
    private boolean isSet;
    private int tungDoDau;
    public static AlertConfirm alertCon;
    public static boolean isStart = true;

    public DrawChoice(Start midlet) {
        this.setFullScreenMode(true);
        bufferImage = Image.createImage(getWidth(), getHeight());
        LEVEL = 0;
        liv = 3;

        KD = new int[10];
        TT = new int[7];
        VD = new int[10];

        hScore = new HighScore(midlet);
        if (alertCon == null) {
            alertCon = new AlertConfirm(midlet);
        }
        choiceWidth = getWidth() - 2 * PAD_LR;
        try {
            logo = Image.createImage("/imgs/logo.png");
            lock = Image.createImage("/imgs/lock.png");
            unlock = Image.createImage("/imgs/unlock.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            if (q_nomal == null) {
                q_nomal = Image.createImage("/imgs/q_nomal.png");
            }
            if (q_hover == null) {
                q_hover = Image.createImage("/imgs/q_hover.png");
            }
            if (q_failed == null) {
                q_failed = Image.createImage("/imgs/q_fail.png");
            }
            if (live == null) {
                live = Image.createImage("/imgs/allow.png");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        padding_top = (choiceHeight - font.getHeight()) / 2;

        startScroll = 0;
        endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;

        g = bufferImage.getGraphics();
        this.midlet = midlet;
        createRecord();
//        createRecord2();
    }

    private void createRecord() {
        RecordStore levelActived = null;
        try {
            levelActived = RecordStore.openRecordStore("LevelActived", false);

            if (levelActived != null && isStart) {
                isStart = false;
                try {
                    byte[] ID = levelActived.getRecord(1);
                    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ID));
                    try {
                        levelPlaying = dis.readInt();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
//                    byte[] ID1 = levelActived.getRecord(2);
//                    DataInputStream dis1 = new DataInputStream(new ByteArrayInputStream(ID1));
//                    int a = 0;
//                    try {
//                        a = dis1.readInt();
//                    } catch (IOException ex) {
//                        ex.printStackTrace();
//                    }
//                    System.out.println(levelPlaying + "&&" + a);
//                    if (a == 1) {
//                        DrawChoice.khoiDong = true;
//                    } else if (a == 2) {
//                        DrawChoice.tangToc = true;
//                    } else if (a == 3){
//                        DrawChoice.veDich = true;
//                    }
                } catch (InvalidRecordIDException ex) {
                    ex.printStackTrace();
                } catch (RecordStoreNotOpenException ex) {
                    ex.printStackTrace();
                } catch (RecordStoreException ex) {
                    ex.printStackTrace();
                }

            }
        } catch (RecordStoreException ex) {
            try {
                levelActived = RecordStore.openRecordStore("LevelActived", true);
            } catch (RecordStoreException ex1) {
                ex1.printStackTrace();
            }
            System.out.println("hihihihihih");
            int i = 0;
            while (i < 2) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream data = new DataOutputStream(baos);
                try {
                    data.writeInt(0);
                    data.flush();
                } catch (IOException ex1) {
                    ex1.printStackTrace();
                }


                byte[] rec = baos.toByteArray();
                try {
                    levelActived.addRecord(rec, 0, rec.length);
                } catch (RecordStoreNotOpenException ex1) {
                    ex1.printStackTrace();
                } catch (InvalidRecordIDException ex1) {
                    ex1.printStackTrace();
                } catch (RecordStoreException ex1) {
                    ex1.printStackTrace();
                }

                try {
                    data.close();
                    baos.close();
                } catch (IOException ex1) {
                    ex1.printStackTrace();
                }
                i++;
            }

        }
        try {
            levelActived.closeRecordStore();
        } catch (RecordStoreNotOpenException ex1) {
            ex1.printStackTrace();
        } catch (RecordStoreException ex1) {
            ex1.printStackTrace();
        }
    }

    private void createRecord2() {
        RecordStore levelActived = null;
        try {
            levelActived = RecordStore.openRecordStore("Image_Names", false);

            if (levelActived != null && isStart) {
                isStart = false;
                if (khoiDong) {
                    for (int i = 0; i < 10; i++) {
                        byte[] ID1 = levelActived.getRecord(i+1);
                        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ID1));
                        try {
                            KD[i] = dis.readInt();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                } else if (tangToc) {
                    for (int i = 0; i < 7; i++) {
                        byte[] ID1 = levelActived.getRecord(i+1);
                        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ID1));
                        try {
                            TT[i] = dis.readInt();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                } else if (veDich) {
                    for (int i = 0; i < 10; i++) {
                        byte[] ID1 = levelActived.getRecord(i+1);
                        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(ID1));
                        try {
                            VD[i] = dis.readInt();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
        } catch (RecordStoreException ex) {
            try {
                levelActived = RecordStore.openRecordStore("Image_Names", true);
            } catch (RecordStoreException ex1) {
                ex1.printStackTrace();
            }
            System.out.println("hihihihihih");
            int i = 0;
            while (i < 10) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DataOutputStream data = new DataOutputStream(baos);
                try {
                    data.writeInt(0);
                    data.flush();
                } catch (IOException ex1) {
                    ex1.printStackTrace();
                }


                byte[] rec = baos.toByteArray();
                try {
                    levelActived.addRecord(rec, 0, rec.length);
                } catch (RecordStoreNotOpenException ex1) {
                    ex1.printStackTrace();
                } catch (InvalidRecordIDException ex1) {
                    ex1.printStackTrace();
                } catch (RecordStoreException ex1) {
                    ex1.printStackTrace();
                }

                try {
                    data.close();
                    baos.close();
                } catch (IOException ex1) {
                    ex1.printStackTrace();
                }
                i++;
            }

        }
        try {
            levelActived.closeRecordStore();
        } catch (RecordStoreNotOpenException ex1) {
            ex1.printStackTrace();
        } catch (RecordStoreException ex1) {
            ex1.printStackTrace();
        }
    }

    public void paint(Graphics g) {
        renderTheWorld();
        g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    public void renderTheWorld() {

        g.setColor(0x00f0dfb6);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.drawImage(logo, getWidth() / 2, 0, Graphics.HCENTER | Graphics.TOP);
        g.setFont(font);

        if (screen == 1) {
            g.setColor(0x00ffffff);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + choiceHeight + PAD_BT - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);

            g.setColor(0x00b0711f);
            if (getLine == 1) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + trans, choiceWidth, choiceHeight, 5, 5);

            g.setColor(0x00b0711f);
            if (getLine == 2) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + choiceHeight + PAD_BT + trans, choiceWidth, choiceHeight, 5, 5);

            g.setColor(0x002C2929);
            if (getLine == 1) {
                firstMenu = 1;
                g.setColor(0x00ffffff);
            }
            g.drawString("Bắt đầu chơi", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + padding_top + trans,
                    Graphics.HCENTER | Graphics.TOP);

            g.setColor(0x002C2929);
            if (getLine == 2) {
                firstMenu = 2;
                g.setColor(0x00ffffff);
            }
            g.drawString("Điểm cao", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + choiceHeight + padding_top + PAD_BT + trans,
                    Graphics.HCENTER | Graphics.TOP);

            g.setColor(0x00b0711f);
            if (getLine == 3) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + 2 * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);
            g.setColor(0x00ffffff);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + 2 * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
            g.setColor(0x002C2929);
            if (getLine == 3) {
                firstMenu = 3;
                g.setColor(0x00ffffff);
            }
            g.drawString("Thông tin", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + 2 * choiceHeight + padding_top + 2 * PAD_BT + trans,
                    Graphics.HCENTER | Graphics.TOP);


            g.setColor(0x00b0711f);
            if (getLine == 4) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + 3 * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);
            g.setColor(0x00ffffff);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + 3 * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
            g.setColor(0x002C2929);
            if (getLine == 4) {
                firstMenu = 4;
                g.setColor(0x00ffffff);
            }
            g.drawString("Gửi tặng", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + 3 * choiceHeight + padding_top + 3 * PAD_BT + trans,
                    Graphics.HCENTER | Graphics.TOP);

            g.setColor(0x00ffffff);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + 4 * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
            g.setColor(0x00b0711f);
            if (getLine == 5) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + 4 * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);

            g.setColor(0x002C2929);
            if (getLine == 5) {
                firstMenu = 5;
                g.setColor(0x00ffffff);
            }
            g.drawString("Thoát", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + 4 * choiceHeight + padding_top + 4 * PAD_BT + trans,
                    Graphics.HCENTER | Graphics.TOP);
        } else {
            levelActivated = RememberLoadedLevel.getRecord();
            System.out.println("fdfd" + levelActivated);
            if (levelActivated == 0) {
                levelActivated = 1;
            }

            if (!isSet) {
                isSet = true;
                endScroll2 = logo.getHeight() + 4 + (levelActivated + 2) * (choiceHeight + PAD_BT) - PAD_BT;
                System.out.println("en=" + endScroll2);
            }

            for (int i = 1; i <= levelActivated; i++) {
                g.setColor(0x00b0711f);
                if (getLine == i) {
                    g.setColor(0x00E99649);
                }
                g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + (i - 1) * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);
                g.setColor(0x00ffffff);
                g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + (i - 1) * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);

                g.setColor(0x002C2929);
                if (getLine == i) {
                    secondMenu = i;
                    g.setColor(0x00ffffff);
                }
                g.drawString("Level " + String.valueOf(i), PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + (i - 1) * (choiceHeight + PAD_BT)
                        + padding_top + trans, Graphics.HCENTER | Graphics.TOP);
                g.drawImage(unlock, PAD_LR + 5 + unlock.getWidth() / 2, logo.getHeight() + 4 + (i - 1) * (choiceHeight + PAD_BT)
                        + choiceHeight / 2 + trans, Graphics.HCENTER | Graphics.VCENTER);
            }

            g.setColor(0x00b0711f);
            if (getLine == levelActivated + 1) {
                g.setColor(0x00E99649);
            }
            g.fillRoundRect(PAD_LR, logo.getHeight() + 4 + levelActivated * (choiceHeight + PAD_BT) + trans, choiceWidth, choiceHeight, 5, 5);
            g.setColor(0x00ffffff);
            g.drawRoundRect(PAD_LR - 1, logo.getHeight() + 4 + levelActivated * (choiceHeight + PAD_BT) - 1 + trans, choiceWidth + 1, choiceHeight + 1, 5, 5);
            g.setColor(0x002C2929);
            if (getLine == levelActivated + 1) {
                secondMenu = levelActivated + 1;
                g.setColor(0x00ffffff);
            }
            g.drawString("Lên đời", PAD_LR + choiceWidth / 2, logo.getHeight() + 4 + levelActivated * (choiceHeight + PAD_BT) + padding_top + trans,
                    Graphics.HCENTER | Graphics.TOP);
            g.drawImage(unlock, PAD_LR + 5 + unlock.getWidth() / 2, logo.getHeight() + 4 + levelActivated * (choiceHeight + PAD_BT)
                    + choiceHeight / 2 + PAD_BT + trans,
                    Graphics.HCENTER | Graphics.VCENTER);

            g.setColor(0x00b0711f);
            font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
            g.setFont(font);
            g.fillRect(0, getHeight() - font.getHeight() - 4, getWidth(), font.getHeight() + 4);
            g.setColor(0x00ffffff);
            g.drawLine(0, getHeight() - font.getHeight() - 4, getWidth(), getHeight() - font.getHeight() - 4);
            g.drawString("Dừng chơi", 1, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);
        }
    }

    protected void pointerPressed(int x, int y) {
        tungDoDau = y;
    }

    protected void pointerReleased(int x, int y) {
        int t = y - tungDoDau;
        if (t < 5 && t > -5) {
            int temp;
            if (x >= 0 && x <= font.stringWidth("Dung choi") + 10) {
                if (y <= getHeight() && y >= getHeight() - font.getHeight() - 10) {
                    screen = 1;
                    secondMenu = 0;
                    repaint();
                }
            }

            if (screen == 1) {
                if (x >= PAD_LR - 1 && x <= PAD_LR + choiceWidth) {
                    temp = logo.getHeight() + 4 + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        screen = 2;
                        repaint();
                    }

                    temp = logo.getHeight() + 4 + trans + choiceHeight + PAD_BT;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        midlet.disManager.pushDisplayable(hScore);
                    }

                    temp = logo.getHeight() + 4 + 2 * (choiceHeight + PAD_BT) + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        midlet.disManager.pushDisplayable(new HDTopMenu(midlet));
                    }

                    temp = logo.getHeight() + 4 + 3 * (choiceHeight + PAD_BT) + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        String[] hd = new String[1];
                        hd[0] = "Đây là tính năng giúp bạn chia sẻ game này cho những người bạn khác."
                                + "Bấm Gửi để nhập vào số điện thoại muốn tặng.";

                        midlet.disManager.pushDisplayable(new TextCanvas(hd, midlet));
                    }

                    temp = logo.getHeight() + 4 + 4 * (choiceHeight + PAD_BT) + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        midlet.destroyApp(true);
                    }
                }
            } else {
                if (x >= PAD_LR - 1 && x <= PAD_LR + choiceWidth) {
                    temp = logo.getHeight() + 4 + trans;
                    if (y >= temp - 1 && y <= temp + choiceHeight) {
                        secondMenu = 0;
                        startScroll = 0;
                        endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;
                        trans = 0;
                        isSet = false;
                        midlet.disManager.pushDisplayable(this);
                        DrawTrialLevel di = new DrawTrialLevel(midlet);
                        midlet.disManager.pushDisplayable(di);
                    } else if (levelActivated > 1 && y >= temp + choiceHeight && y <= temp + levelActivated * (choiceHeight + PAD_BT)) {
                        secondMenu = (y - temp) / (choiceHeight + PAD_BT) + 1;
                        if (levelPlaying != secondMenu) {
                            trans = 0;
                            startScroll = 0;
                            isSet = false;
                            endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;
                            midlet.disManager.emptyStack();
                            midlet.disManager.pushDisplayable(this);
                            midlet.disManager.pushDisplayable(new DownloadAlert(midlet, secondMenu));
                        } else {
                            levelPlaying = secondMenu;
                            secondMenu = 0;
                            trans = 0;
                            startScroll = 0;
                            isSet = false;
                            endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;
                            midlet.disManager.pushDisplayable(this);
                            midlet.disManager.pushDisplayable(new DrawChoiceActivated(midlet));
                        }
                    } else {
                        temp = logo.getHeight() + 4 + trans + levelActivated * (choiceHeight + PAD_BT);
                        if (y >= temp - 1 && y <= temp + choiceHeight) {
                            secondMenu = (temp - logo.getHeight() - 4 - trans) / (choiceHeight + PAD_BT) + 1;
                            screen = 2;
                            trans = 0;
                            startScroll = 0;
                            endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;
                            isSet = false;
                            midlet.disManager.emptyStack();
                            midlet.disManager.pushDisplayable(this);
                            midlet.disManager.pushDisplayable(new DownloadAlert(midlet, secondMenu));
                        }
                    }
                }
            }
        } else {
            if (t > 0) {
                if (startScroll < 0) {
                    if (screen == 1) {
                        endScroll1 += 20;
                    } else {
                        endScroll2 += 20;
                    }
                    startScroll += 20;
                    if (startScroll > 0) {
                        trans = startScroll = 0;
                    } else {
                        trans += 20;
                    }
                }
            } else {
                if (endScroll1 > getHeight() - 5 && screen == 1) {
                    endScroll1 -= 20;
                    startScroll -= 20;
//                    g.translate(0, -5);
                    trans -= 20;
                }
                if (endScroll2 > (getHeight() - font.getHeight() - 8) && screen == 2) {
                    endScroll2 -= 20;
                    startScroll -= 20;
//                    g.translate(0, -5);
                    trans -= 20;
                }
            }
            if (screen == 1) {
                repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2, getHeight() - logo.getHeight() - 3);
            } else {
                repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2,
                        getHeight() - logo.getHeight() - 3 - font.getHeight() - 4);
            }
        }
    }

    protected void keyPressed(int keyCode) {
//        int action = getGameAction(keyCode);
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);
        switch (instanceKeyCode) {
            case KeyCodeAdapter.KEY_2:
            case KeyCodeAdapter.UP_KEY:
                getLine--;
                if (getLine == 0) {
                    getLine = 1;
                }
                if (startScroll < 0) {
                    if (screen == 1) {
                        endScroll1 += 10;
                    } else {
                        endScroll2 += 10;
                    }
                    startScroll += 10;
//                    g.translate(0, 5);
                    trans += 10;
                }
                if (screen == 1) {
                    repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2, getHeight() - logo.getHeight() - 3);
                } else {
                    repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2,
                            getHeight() - logo.getHeight() - 3 - font.getHeight() - 4);
                }
                break;

            case KeyCodeAdapter.KEY_8:
            case KeyCodeAdapter.DOWN_KEY:
                getLine++;
                if (screen == 1) {
                    if (getLine == 6) {
                        getLine = 5;
                    }
                } else {
                    if (getLine == levelActivated + 2) {
                        getLine = levelActivated + 1;
                    }
                }
                if (endScroll1 > getHeight() - 5 && screen == 1) {
                    endScroll1 -= 10;
                    startScroll -= 10;
//                    g.translate(0, -5);
                    trans -= 10;
                }
                if (endScroll2 > (getHeight() - font.getHeight() - 8) && screen == 2) {
                    endScroll2 -= 10;
                    startScroll -= 10;
//                    g.translate(0, -5);
                    trans -= 10;
                }
                if (screen == 1) {
                    repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2, getHeight() - logo.getHeight() - 3);
                } else {
                    repaint(PAD_LR - 1, logo.getHeight() + 4 - 1, choiceWidth + 2,
                            getHeight() - logo.getHeight() - 3 - font.getHeight() - 4);
                }
                break;

            case KeyCodeAdapter.KEY_5:
            case KeyCodeAdapter.CENTER_KEY:
                trans = 0;
                screen = 1;
                isSet = false;
                getLine = 1;
                startScroll = 0;
                endScroll1 = logo.getHeight() + 4 + 5 * choiceHeight + 4 * PAD_BT;
                System.out.println("level" + levelPlaying);

                if (firstMenu == 1) {
                    screen = 2;
                    repaint();
                } else if (firstMenu == 2) {
                    midlet.disManager.pushDisplayable(hScore);
                } else if (firstMenu == 3) {
                    midlet.disManager.pushDisplayable(new HDTopMenu(midlet));
                } else if (firstMenu == 4) {
                    String[] hd = new String[1];
                    hd[0] = "Đây là tính năng giúp bạn chia sẻ game này cho những người bạn khác."
                            + "Bấm Gửi để nhập vào số điện thoại muốn tặng.";
//                    midlet.disManager.pushDisplayable(new GameFriendly(midlet));
                    midlet.disManager.pushDisplayable(new TextCanvas(hd, midlet));
                } else if (firstMenu == 5) {
                    midlet.destroyApp(true);
                }

                if (secondMenu == 1) {
                    secondMenu = 0;
                    midlet.disManager.pushDisplayable(this);
                    DrawTrialLevel di = new DrawTrialLevel(midlet);
                    midlet.disManager.pushDisplayable(di);
                } else if (secondMenu > 1 && secondMenu < levelActivated + 1) {
                    if (levelPlaying != secondMenu) {
                        midlet.disManager.emptyStack();
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DownloadAlert(midlet, secondMenu));
                    } else {
                        secondMenu = 0;
                        midlet.disManager.pushDisplayable(this);
                        midlet.disManager.pushDisplayable(new DrawChoiceActivated(midlet));
                    }
                } else if (secondMenu == levelActivated + 1) {
                    screen = 2;
                    midlet.disManager.emptyStack();
                    midlet.disManager.pushDisplayable(this);
                    midlet.disManager.pushDisplayable(new DownloadAlert(midlet, secondMenu));
                }
                break;

            case KeyCodeAdapter.SOFT_KEY_LEFT:
                screen = 1;
                secondMenu = 0;
                repaint();
                break;
        }
    }
}
